package com.ruoyi.web.controller.business;

import com.ruoyi.business.bo.HcxCarWashAddBo;
import com.ruoyi.business.bo.HcxCarWashEditBo;
import com.ruoyi.business.bo.HcxCarWashQueryBo;
import com.ruoyi.business.service.IHcxCarWashService;
import com.ruoyi.business.vo.HcxCarWashVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 洗车卡设置Controller
 *
 * @author wsdz
 * @date 2022-08-17
 */
@Api(value = "洗车卡设置控制器", tags = {"洗车卡设置管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/business/CarWash")
public class HcxCarWashController extends BaseController {

    private final IHcxCarWashService iHcxCarWashService;

    /**
     * 查询洗车卡设置列表
     */
    @ApiOperation("查询洗车卡设置列表")
    @PreAuthorize("@ss.hasPermi('business:CarWash:list')")
    @GetMapping("/list")
    public TableDataInfo<HcxCarWashVo> list(@Validated HcxCarWashQueryBo bo) {
        return iHcxCarWashService.queryPageList(bo);
    }

    /**
     * 导出洗车卡设置列表
     */
    @ApiOperation("导出洗车卡设置列表")
    @PreAuthorize("@ss.hasPermi('business:CarWash:export')")
    @Log(title = "洗车卡设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated HcxCarWashQueryBo bo) {
        List<HcxCarWashVo> list = iHcxCarWashService.queryList(bo);
        ExcelUtil<HcxCarWashVo> util = new ExcelUtil<>(HcxCarWashVo.class);
        return util.exportExcel(list, "洗车卡设置");
    }

    /**
     * 获取洗车卡设置详细信息
     */
    @ApiOperation("获取洗车卡设置详细信息")
    @PreAuthorize("@ss.hasPermi('business:CarWash:query')")
    @GetMapping("/{id}")
    public AjaxResult<HcxCarWashVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iHcxCarWashService.queryById(id));
    }

    /**
     * 新增洗车卡设置
     */
    @ApiOperation("新增洗车卡设置")
    @PreAuthorize("@ss.hasPermi('business:CarWash:add')")
    @Log(title = "洗车卡设置", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HcxCarWashAddBo bo) {
        return toAjax(iHcxCarWashService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改洗车卡设置
     */
    @ApiOperation("修改洗车卡设置")
    @PreAuthorize("@ss.hasPermi('business:CarWash:edit')")
    @Log(title = "洗车卡设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HcxCarWashEditBo bo) {
        return toAjax(iHcxCarWashService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除洗车卡设置
     */
    @ApiOperation("删除洗车卡设置")
    @PreAuthorize("@ss.hasPermi('business:CarWash:remove')")
    @Log(title = "洗车卡设置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iHcxCarWashService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入洗车卡设置数据
     */
    @ApiOperation("导入洗车卡设置数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('business:CarWash:import')")
    @Log(title = "洗车卡设置" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<HcxCarWashVo> util = new ExcelUtil<>(HcxCarWashVo.class);
        List<HcxCarWashVo> hcxCarWashList;
        try {
            hcxCarWashList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iHcxCarWashService.importHcxCarWash(hcxCarWashList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HcxCarWashVo> util = new ExcelUtil<>(HcxCarWashVo.class);
        return util.importTemplateExcel("洗车卡设置数据");
    }
}
